package com.ruoyi.web.controller.fs;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.fs.domain.FsCase;
import com.ruoyi.fs.domain.FsCaseExpress;
import com.ruoyi.fs.domain.FsExpressInfo;
import com.ruoyi.fs.service.IFsCaseExpressService;
import com.ruoyi.fs.service.IFsCaseService;
import com.ruoyi.fs.service.IFsExpressInfoService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsWorkOrderPost;
import com.ruoyi.fs.service.IFsWorkOrderPostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 邮寄工单Controller
 *
 * @author hero
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/fs/post")
public class FsWorkOrderPostController extends BaseController {
    @Autowired
    private IFsWorkOrderPostService fsWorkOrderPostService;
    @Autowired
    private IFsCaseService fsCaseService;
    @Autowired
    private IFsCaseExpressService fsCaseExpressService;
    @Autowired
    private IFsExpressInfoService fsExpressInfoService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询邮寄工单列表
     */
    @PreAuthorize("@ss.hasPermi('fs:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsWorkOrderPost fsWorkOrderPost) {
        startPage();
        List<FsWorkOrderPost> list = fsWorkOrderPostService.selectFsWorkOrderPostList(fsWorkOrderPost);
        for (int i = 0; i < list.size(); i++) {
            FsWorkOrderPost post = list.get(i);
            Long cjr = post.getCjr();
            if (cjr != null) {
                SysUser user = sysUserService.selectUserById(cjr);
                post.setCjrMc(user.getNickName());
            }
            Long ssr = post.getSsr();
            if (ssr != null) {
                SysUser user = sysUserService.selectUserById(ssr);
                post.setSsrMc(user.getNickName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出邮寄工单列表
     */
    @PreAuthorize("@ss.hasPermi('fs:post:export')")
    @Log(title = "邮寄工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsWorkOrderPost fsWorkOrderPost) {
        List<FsWorkOrderPost> list = fsWorkOrderPostService.selectFsWorkOrderPostList(fsWorkOrderPost);
        ExcelUtil<FsWorkOrderPost> util = new ExcelUtil<FsWorkOrderPost>(FsWorkOrderPost.class);
        util.exportExcel(response, list, "邮寄工单数据");
    }

    /**
     * 获取邮寄工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:post:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fsWorkOrderPostService.selectFsWorkOrderPostById(id));
    }

    /**
     * 新增邮寄工单
     */
    @PreAuthorize("@ss.hasPermi('fs:post:add')")
    @Log(title = "邮寄工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsWorkOrderPost fsWorkOrderPost) {
        return toAjax(fsWorkOrderPostService.insertFsWorkOrderPost(fsWorkOrderPost));
    }

    /**
     * 修改邮寄工单
     */
    @PreAuthorize("@ss.hasPermi('fs:post:edit')")
    @Log(title = "邮寄工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsWorkOrderPost fsWorkOrderPost) {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        if (user != null) {
            fsWorkOrderPost.setSsr(user.getUserId());
        }
        fsWorkOrderPost.setZt(3);
        fsWorkOrderPost.setWcsj(new Date());
        FsExpressInfo fsExpressInfo = new FsExpressInfo();
        fsExpressInfo.setKddh(fsWorkOrderPost.getKddh());
        List<FsExpressInfo> olds = fsExpressInfoService.selectFsExpressInfoList(fsExpressInfo);
        if (olds.isEmpty()) {
            fsExpressInfo.setCom(fsWorkOrderPost.getCom());
            fsExpressInfo.setKddh(fsWorkOrderPost.getKddh());
            fsExpressInfo.setSjr(fsWorkOrderPost.getSjr());
            fsExpressInfo.setDh(fsWorkOrderPost.getDh());
            fsExpressInfo.setDz(fsWorkOrderPost.getDz());
            fsExpressInfo.setRemark(fsWorkOrderPost.getYjyq());
            fsExpressInfo.setYwlx(fsWorkOrderPost.getYwlx());
            fsExpressInfo.setYwzt(1);
            fsExpressInfo.setGxsj(new Date());
            fsExpressInfo.setFjsj(new Date());
            fsExpressInfoService.insertFsExpressInfo(fsExpressInfo);
        } else {
            fsExpressInfo = olds.get(0);
        }
        FsCaseExpress caseExpress = new FsCaseExpress();
        caseExpress.setCaseId(fsWorkOrderPost.getCaseId());
        caseExpress.setExpressId(fsExpressInfo.getId());
        if (fsCaseExpressService.selectFsCaseExpressList(caseExpress).isEmpty()) {
            fsCaseExpressService.insertFsCaseExpress(caseExpress);
        }

        FsCase fsCase = fsCaseService.selectFsCaseById(fsWorkOrderPost.getCaseId());
        if (fsCase != null) {
            fsCase.setIfPost(1);
            if (fsWorkOrderPost.getYwlx() == 1) {
                fsCase.setLafs(1);
            }
            fsCaseService.updateFsCase(fsCase);
        }
        fsWorkOrderPost.setExpressId(fsExpressInfo.getId());
        fsWorkOrderPostService.updateFsWorkOrderPost(fsWorkOrderPost);
        return success();
    }

    /**
     * 删除邮寄工单
     */
    @PreAuthorize("@ss.hasPermi('fs:post:remove')")
    @Log(title = "邮寄工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fsWorkOrderPostService.deleteFsWorkOrderPostByIds(ids));
    }


    /**
     * 开始工单
     */
    @PreAuthorize("@ss.hasPermi('fs:post:query')")
    @GetMapping(value = "/start/{id}")
    public AjaxResult start(@PathVariable("id") Long id) {
        FsWorkOrderPost post = fsWorkOrderPostService.selectFsWorkOrderPostById(id);
        post.setZt(2);
        return success(fsWorkOrderPostService.updateFsWorkOrderPost(post));
    }

    /**
     * 开始工单
     */
    @PreAuthorize("@ss.hasPermi('fs:post:query')")
    @GetMapping(value = "/wl/{id}")
    public AjaxResult wl(@PathVariable("id") Long id) {
        FsWorkOrderPost post = fsWorkOrderPostService.selectFsWorkOrderPostById(id);
        if (post.getExpressId() != null) {
            FsExpressInfo info = fsExpressInfoService.selectFsExpressInfoById(post.getExpressId());
            return success(info);
        }
        return AjaxResult.error("没有找到物流信息，请联系管理员！");
    }
}
